package com.goldsign.pay.provider.user.util;


import com.goldsign.pay.provider.user.to.UserLoginRespDTO;
import com.goldsign.pay.common.log.SysLog;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 
 * @author xjj
 *
 */
@Component
public class AppTokenManager {
	private static long EXPIRE_TIME=24*60*60*1000;
	private static int HOUR=0;


	@Value("${tokenExpireTime}")
	public void setDatabase(String tokenExpireTime) {
		if(null!=tokenExpireTime||!"".equals(tokenExpireTime)){
			try{
				HOUR = Integer.parseInt(tokenExpireTime);
				SysLog.info("当前设定的token超时时间是"+HOUR);
			}catch(Exception e){

			}
		}

	}


	protected ConcurrentHashMap<String, AppLoginToken> tokenMap() {
		return TokenMap;
	}

	protected void setTokenMap(ConcurrentHashMap<String, AppLoginToken> tokenMap){
		TokenMap=tokenMap;
	}

	protected ConcurrentHashMap<String,String> getUser2Tokens() {
		return user2Tokens;
	}

	protected void setUser2Tokens(ConcurrentHashMap<String,String> user2) {
		user2Tokens=user2;
	}


	/**使用ConcurrentHashMap能够线程安全，而且并发效率比较高**/
	private static ConcurrentHashMap<String,AppLoginToken> TokenMap=new ConcurrentHashMap<String,AppLoginToken>();

	/**使用这个来控制一个user，只能在app手机登录一次，不能同时多设备登录app， key是userName，value是token**/
	private static ConcurrentHashMap<String,String> user2Tokens=new ConcurrentHashMap<String,String>();

	/**
	 * 这个方法用来校验用户是否已经登录成功
	 * @param token
	 * @return
	 * @throws Exception
	 */
	public static boolean validateToken(String token)throws Exception{
		AppLoginToken appLoginToken = TokenMap.get(token);
		//拿不到token校验失败
		if(null==appLoginToken){
			return false;
		}

		long expireTime=0L;
		//做成可配置的
		if(HOUR>0){
			expireTime=60*60*1000*HOUR;
			if(appLoginToken.isExpire(expireTime)){
				return false;
			}
		}else{
			//如果token的超时，校验失败
			if(appLoginToken.isExpire(EXPIRE_TIME)){
				return false;
			}
		}

		//校验成功，需要active()
		appLoginToken.active();
		return true;
	}

	/**
	 * 新增用户登录token
	 * @param userName
	 * @param newToken
	 */
	public static void addNewToken(String userName,AppLoginToken newToken){
		removeToken(userName);
		user2Tokens.put(userName, newToken.getTokenId());
		TokenMap.put(newToken.getTokenId(), newToken);
	}

	/**
	 * 先删除原有的token信息
	 * @param userName
	 */
	public static void removeToken(String userName){
		//1先删除原先的登录信息，
		String oldTokenString = user2Tokens.remove(userName);
		if(null==oldTokenString){
			return;
		}
		TokenMap.remove(oldTokenString);
	}


	public static  AppLoginToken getToken(String tokenId){
		//1先删除原先的登录信息，
		AppLoginToken appLoginToken = TokenMap.get(tokenId);
		return appLoginToken;
	}

	public static UserLoginRespDTO getLoginUser(String tokenId){
		//1先删除原先的登录信息，
		AppLoginToken appLoginToken = TokenMap.get(tokenId);
		if(null==appLoginToken){
			return null;
		}
		return (UserLoginRespDTO)appLoginToken.getAttrubute("LoginUser");
	}
	public static UserLoginRespDTO getLoginUser(HttpServletRequest request){
		String tokenId = request.getParameter("tokenId");
		return getLoginUser(tokenId);
	}

	public static void showTokens(){
		Set<Map.Entry<String, AppLoginToken>> entrySet = TokenMap.entrySet();
		for(Map.Entry<String, AppLoginToken> entry:entrySet){
			SysLog.info("tokenId:"+entry.getKey()+" user:"+entry.getValue().getPhone());
		}
	}
    
}
